/*
 * The MIT License (MIT)
 *
 * FXGL - JavaFX Game Library
 *
 * Copyright (c) 2015-2016 AlmasB (almaslvl@gmail.com)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.windea.study.fxgl.tankwar

import com.almasb.fxgl.dsl.*
import com.almasb.fxgl.entity.*
import com.almasb.fxgl.entity.component.*
import com.almasb.fxgl.entity.components.*
import com.almasb.fxgl.texture.*
import javafx.geometry.*

/**
 * @author Almas Baimagambetov (AlmasB) (almaslvl@gmail.com)
 */
class PlayerComponent : Component() {
	private lateinit var view: ViewComponent
	private lateinit var texture: Texture
	private var speed = 0.0
	private var frames = 0
	
	override fun onAdded() {
		getEntity().transformComponent.rotationOrigin = Point2D(42.0, 42.0)
		texture = FXGL.getAssetLoader().loadTexture("player.png")
		view.addChild(texture)
	}
	
	override fun onUpdate(tpf: Double) {
		speed = tpf * 60
		
		var frame = frames / 10
		if(frame >= 8) {
			frame = 0
			frames = 0
		}
		
		texture.viewport = Rectangle2D((frame * 84).toDouble(), 0.0, 84.0, 84.0)
	}
	
	fun up() {
		getEntity().rotation = 270.0
		getEntity().translateY(-5 * speed)
		frames++
	}
	
	fun down() {
		getEntity().rotation = 90.0
		getEntity().translateY(5 * speed)
		frames++
	}
	
	fun left() {
		getEntity().rotation = 180.0
		getEntity().translateX(-5 * speed)
		frames++
	}
	
	fun right() {
		getEntity().rotation = 0.0
		getEntity().translateX(5 * speed)
		frames++
	}
	
	fun shoot() {
		FXGL.spawn("Bullet", SpawnData(getEntity().center).put("direction", angleToVector()))
	}
	
	private fun angleToVector(): Point2D {
		return when(getEntity().rotation) {
			0.0 -> Point2D(1.0, 0.0)
			90.0 -> Point2D(0.0, 1.0)
			180.0 -> Point2D(-1.0, 0.0)
			270.0 -> Point2D(0.0, -1.0)
			else -> throw IllegalArgumentException()
		}
	}
}
